import { ArrowHelper, Vector3, Object3D, CanvasTexture, SpriteMaterial, Sprite } from './three.custom.js';

// 清空object中，object3Dtype类型的children ( remove + dispose )
//"Bone", "InstancedMesh", "Line", "LineLoop","LOD","Points","Skeleton","SkinnedMesh"
const object3Dtype = ["Object3D", "LineSegments", "Mesh", "Group", "Sprite", "LineSegments2"]
export function clearObject3D(object) {
  for (let i = 0; i < object.children.length; i++) {
    let item = object.children[i]
    if (item.children) {
      clearObject3D(item);
    }
    if (object3Dtype.includes(item.type)) {
      // 从父节点移除
      if (item.parent) {
        item.parent.remove(item);
        i--; // 移除后调整数组中元素的下标对应关系
      }
      // dispose
      if (item.geometry) {
        item.geometry.dispose();
      }
      if (item.material) {
        if (Array.isArray(item.material)) {
          item.material.forEach(materialItem => materialItem.dispose());
        } else {
          item.material.dispose();
        }
      }
    }
  }
}

// 数组去重
export function unique(arr) {
  const obj = {};
  return arr.filter((item) => {
    const a = Object.prototype.hasOwnProperty.call(obj, (typeof item + item)) ? false : (obj[typeof item + item] = true);
    return a;
  });
}

// 根据config更新defaultData配置参数：用于mousekeys
export function updateConfig(config, defaultData) {
  for (const style in config) {
    // 对config中有的ROTATE/DOLLY/PAN进行更新，没有使用默认值
    for (const configItem in defaultData[style]) {
      if (config[style][configItem] !== undefined) {
        defaultData[style][configItem] = config[style][configItem]; // 对于config中定义了的configItem进行更新
      } else if (configItem === 'mouse') {
        defaultData[style].mouse = null; // 对于config中没有定义mouse的，配置无效，该style不生效
      } else {
        defaultData[style][configItem] = false; // 对于config中没有定义其他配置项的，如altKey没有定义，则将其置为false
      }
    }
  }
  return defaultData;
}

// 框选框渲染
export function selectionBoxHelper() {
  let selectionBoxElement = document.createElement('div');
  selectionBoxElement.classList.add('selectBox');
  selectionBoxElement.style.pointerEvents = 'none';
  return selectionBoxElement;
}

// 构造坐标轴
export function createCoordinateSystem(axesLength) {
  const AXIS_COLOR_X = 0xff0000;  // red
  const AXIS_COLOR_Y = 0x13ce66;  // green
  const AXIS_COLOR_Z = 0x0000ff;  // blue

  const ORIGIN = new Vector3(0, 0, 0);
  const AXIS_DIR_X = new Vector3(1, 0, 0);
  const AXIS_DIR_Y = new Vector3(0, 1, 0);
  const AXIS_DIR_Z = new Vector3(0, 0, 1);

  // 坐标轴
  const ARROW_X = new ArrowHelper(AXIS_DIR_X, ORIGIN, axesLength, AXIS_COLOR_X, axesLength / 5, axesLength / 7);
  const ARROW_Y = new ArrowHelper(AXIS_DIR_Y, ORIGIN, axesLength, AXIS_COLOR_Y, axesLength / 5, axesLength / 7);
  const ARROW_Z = new ArrowHelper(AXIS_DIR_Z, ORIGIN, axesLength, AXIS_COLOR_Z, axesLength / 5, axesLength / 7);

  // 坐标轴xyz文字标识
  const TEXT_X = createAxesXYZ('x', '#000000', new Vector3(axesLength * 1.3, 0, 0));
  const TEXT_Y = createAxesXYZ('y', '#000000', new Vector3(0, axesLength * 1.3, 0));
  const TEXT_Z = createAxesXYZ('z', '#000000', new Vector3(0, 0, axesLength * 1.3));

  let coordinateSystem = new Object3D();
  coordinateSystem.add(ARROW_X, ARROW_Y, ARROW_Z, TEXT_X, TEXT_Y, TEXT_Z);

  return coordinateSystem;
}

// 构造坐标轴syz文字标识
function createAxesXYZ(text, color, position) {
  const TEXT_SIZE = 12;

  let canvas = document.createElement('canvas');
  canvas.width = TEXT_SIZE;
  canvas.height = TEXT_SIZE;
  let ctx = canvas.getContext('2d');
  ctx.fillStyle = color;
  ctx.font = TEXT_SIZE + 'px Arial';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';
  ctx.fillText(text, TEXT_SIZE / 2, TEXT_SIZE / 2);

  let texture = new CanvasTexture(canvas);
  let material = new SpriteMaterial({ map: texture });
  let textSprite = new Sprite(material);
  textSprite.scale.set(0.2357, 0.2357, 1); // 设置缩放尺寸，使得投影后大小与canvas大小基本一致，从而减少失真
  textSprite.position.copy(position);
  return textSprite;
}


